package com.code.zjm.intent.demo;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private Intent intent = new Intent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //启动activity的几种方式
        //法1：Component Name
        findViewById(R.id.btStartByComp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.setComponent(new ComponentName("com.code.zjm.intent.demo", "com.code.zjm.intent.demo.SecondActivity"));
                startActivity(intent);
            }
        });
        //法2：使用Action
        findViewById(R.id.btStartByAction).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.setAction("com.code.zjm.intent.demo.intent.action.SECOND");
                startActivity(intent);
            }
        });
        //法3：使用Data
        findViewById(R.id.btStartByData).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File("/storage/sdcard/1.png");
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setDataAndType(Uri.fromFile(file), "image/*");
                startActivity(i);
            }
        });

        //拨打电话
        findViewById(R.id.btCall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse("tel:10086"));
                startActivity(i);
            }
        });

        //打开网页
        findViewById(R.id.btOpenWebView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
                startActivity(i);
            }
        });


    }
}
